package Dao;

import entity.Mail;
import utils.JdbcUtils_DBCP;

import javax.swing.*;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class MailDaoImpl implements MailDao {
    private Connection connect;
    private PreparedStatement preparedStatement;
    private ArrayList<Mail> data;
    private JdbcUtils_DBCP util;



    public MailDaoImpl()
    {
        this.util = new JdbcUtils_DBCP();
        try {
            this.connect = JdbcUtils_DBCP.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //获取快件集合
        this.data = new ArrayList<Mail>();
    }

    //插入新的订单
    public void Insert(Mail order)
    {
        //插入用的SQL语句
        //初始化就需要设置的列(INSERT INTO后表名+列 ,要按顺序对应，占位符的数量也要相同，不然会产生越界错误
        String InsertSQL = "INSERT INTO mail(mid,uid,mname,arrivaltime,completiontime,mailstate)"
                + "VALUES(?,?,?,?,?,?)";
        try
        {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            Date ArrivalTime = simpleDateFormat.parse(order.getArrivalTime());
            Timestamp sqlDateArrival = new Timestamp(ArrivalTime.getTime());

            Date CompletionTime = simpleDateFormat.parse(order.getCompletionTime());
            Timestamp sqlDateCompletion = new Timestamp(CompletionTime.getTime());


            this.preparedStatement = this.connect.prepareStatement(InsertSQL);
            this.preparedStatement.setString(1, order.getmId());
            this.preparedStatement.setString(2, order.getuId());
            this.preparedStatement.setString(3, order.getmName());
            this.preparedStatement.setTimestamp(4, sqlDateArrival);
            this.preparedStatement.setTimestamp(5, sqlDateCompletion);
            this.preparedStatement.setString(6, order.getMailState());

            this.preparedStatement.executeUpdate();
            JOptionPane.showMessageDialog(null, "快件添加成功");

            this.preparedStatement.close();
        }

         catch(SQLException e)
        {
            JOptionPane.showMessageDialog(null, "收件人ID错误");
        }catch (IllegalArgumentException e){
            JOptionPane.showMessageDialog(null, "快件添加失败，请输入正确的格式，如时间:2021-09-15 12:00:00");
            e.printStackTrace();
        } catch (ParseException e) {
            JOptionPane.showMessageDialog(null, "快件添加失败，请输入正确的格式，如时间:2021-09-15 12:00:00");
            e.printStackTrace();
        }
    }

    public void Delete(String d_id)
    {

        String DeleteSQL = "DELETE FROM mail "+ "WHERE mid = ?";
        try
        {
            this.preparedStatement = this.connect.prepareStatement(DeleteSQL);
            this.preparedStatement.setString(1, d_id);
            this.preparedStatement.executeUpdate();
            this.preparedStatement.close();
            JOptionPane.showMessageDialog(null, "快件删除成功");
        }
        catch(SQLException e)
        {
            JOptionPane.showMessageDialog(null, "快件删除失败，请输入正确的快件ID");
            e.printStackTrace();
        }
    }

    public void UpData(String uid, String mid, String arrivaltime, String mailstate, String Completiontime, String mname)
    {



        String UpdataSQL = "UPDATE mail SET uid=? ,  arrivaltime=? , mailstate=? , Completiontime =? , mname=?  WHERE mid = ?";
        try
        {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            Date ArrivalTime = simpleDateFormat.parse(arrivaltime);
            Timestamp sqlDateArrival = new Timestamp(ArrivalTime.getTime());

            Date CompletionTime = simpleDateFormat.parse(Completiontime);
            Timestamp sqlDateCompletion = new Timestamp(CompletionTime.getTime());
            this.preparedStatement = this.connect.prepareStatement(UpdataSQL);
            this.preparedStatement.setString(1, uid);
            this.preparedStatement.setTimestamp(2, sqlDateArrival);
            this.preparedStatement.setString(3, mailstate);
            this.preparedStatement.setTimestamp(4, sqlDateCompletion);
            this.preparedStatement.setString(5, mname);
            this.preparedStatement.setString(6, mid);
            this.preparedStatement.executeUpdate();
            JOptionPane.showMessageDialog(null, "快件修改成功");
            this.preparedStatement.close();
        }
        catch(SQLException e)
        {
            JOptionPane.showMessageDialog(null, "收件人ID错误");
        }catch (IllegalArgumentException e){
            JOptionPane.showMessageDialog(null, "快件添加失败，请输入正确的格式，如时间:2021-09-15 12:00:00");
            e.printStackTrace();
        } catch (ParseException e) {
            JOptionPane.showMessageDialog(null, "快件添加失败，请输入正确的格式，如时间:2021-09-15 12:00:00");
            e.printStackTrace();
        }

    }

}
